package com.websocket.quotation;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.system.comm.utils.FrameNoUtil;
import com.websocket.data.WebSocketData;
import com.websocket.handler.WebSocketMsgHandler;
import com.websocket.pojo.User;

/**
 * 发送行情信息
 * @author JiMMY 
 * @date 2021年5月16日 下午4:47:45
 */
@Component
public class QuotationSend {

	private static final Logger LOGGER = LoggerFactory.getLogger(QuotationSend.class);
	@Autowired
	private WebSocketMsgHandler webSocketHandler;
	@Autowired
	private WebSocketData webSocketData;

	/**
	 * 初始函数
	 * @param interval	推送间隔时间，单位ms
	 */
	@Scheduled(fixedDelayString = "${project.quotation.send.interval}")
	public void init() {
		List<String> list = webSocketData.getQuotationUserList();
		for (String sessionId : list) {
			User user = webSocketData.getUser(sessionId);
			if (user == null) {
				continue;
			}
			String message = user.getToken() + "-" + user.getSessionId() + ":回复消息-" + FrameNoUtil.uuid();
			try {
				webSocketHandler.sendMessage(sessionId, message);
				LOGGER.info("{}-发送消息:{}", user.getToken(), message);
			} catch (Exception e) {
				LOGGER.error(user.getToken() + "-发送消息失败:" + message, e);
			}
		}
	}
}